import React from "react";
import {message, Select} from "antd";
import {dataBookApi, menuApi} from "../../services/basic";

// 用于兼容旧版本的数据字典
// 原文件：src\components\BtnPermission\newComponent.js
class DicComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            permission: {}, // 按钮权限
            dataBook: {}, // 数据字典
        }
    }

    /*    // 数据字典
        // sname,  字段名称
        // list，数据列表，可不传
        bookRadioRender = (sname, list) => {
            // 没传字段名称
            if (!sname) {
                return [];
            }
            const listData = list || this.state.dataBook[sname];
            // 没有当前字段数据
            if (!listData) {
                return [];
            }
            return listData.map((val) => (
                <Radio.Button key={val.sjbm} value={val.sjbm}>
                    {val.sjmc}
                </Radio.Button>
            ));
        };*/

    // 数据字典请求
    getDataBook = () => {
        const keys = Object.keys(this.state.dataBook);
        const params = {
            zdlxdmIN: keys.join(","),
            ztbz: 1,
        };
        dataBookApi
            .getByDm(params)
            .then((res) => {
                if (res.code === "1") {
                    const {data} = res;
                    const hash = {};
                    data.forEach((element) => {
                        const sjbm = element.sjbm;
                        if (this.state.dataBook[sjbm]) {
                            hash[this.state.dataBook[sjbm]] = element.sjzds;
                        }
                    });
                    this.setState((state) => ({...state, dataBook: hash}));
                }
            })
            .catch((error) => {
                // eslint-disable-next-line
                console.dir(error);
            });
    };
    // 数据字典
    // sname,  字段名称
    // isNum，选项 value类型，true: 数字，false: 字符串，   默认 true
    // list，数据列表，可不传
    bookRender = (sname, isNum = true, list) => {
        // 没传字段名称
        if (!sname) {
            return [];
        }
        const listData = list || this.state.dataBook[sname];
        // 没有当前字段数据
        if (!listData) {
            return [];
        }
        return listData.map((val) => (
            <Select.Option
                key={val.sjbm}
                value={isNum ? Number(val.sjbm) : String(val.sjbm)}
            >
                {val.sjmc}
            </Select.Option>
        ));
    };

    // 自定义 sessionStorage 中取值的方法
    getSessionItem = (item, name) => {
        const sesItem = JSON.parse(sessionStorage.getItem(item));
        if (!sesItem) {
            message.warning("sessionStorage数据缺失, 请刷新页面或重新登录");
        }
        if (name) {
            return sesItem[name];
        }
        return sesItem;
    };

    /*    // 弹窗按钮权限
        modalfoot = (handler, hideModal, handleOk) => {
            return handler === "view"
                ? [
                    <Button key="back" onClick={hideModal}>
                        关闭
                    </Button>,
                ]
                : handler === "qr"
                    ? [
                        <Button key="back" onClick={hideModal}>
                            取消
                        </Button>,
                        <Button key="submit" type="primary" onClick={handleOk}>
                            确认
                        </Button>,
                    ]
                    : [
                        <Button key="back" onClick={hideModal}>
                            取消
                        </Button>,
                        <Button key="submit" type="primary" onClick={handleOk}>
                            保存
                        </Button>,
                    ];
        };*/

    // 查询菜单的按钮权限
    getBtnPermission = (id) => {
        menuApi.getBtns(id).then((res) => {
            const btns = {
                增加: "add",
                删除: "delete",
                修改: "edit",
                查询: "review",
            };
            if (res.code === "1") {
                const perm = {};
                const {data} = res;
                for (const item of data) {
                    const name = item.mc;
                    if (btns[name]) {
                        perm[btns[name]] = true;
                    }
                }
                this.setState((state) => ({...state, permission: perm}));
            }
        })
            .catch((error) => {
                // eslint-disable-next-line
                console.dir(error);
            });
    };

    componentDidMount() {
        // 数据字典
        if (this.state.dataBook) {
            const keys = Object.keys(this.state.dataBook);
            this.getDataBook({
                zdlxdmIN: keys.join(","),
                ztbz: 1,
            });
        }
        // 菜单权限
        if (!this.state.permission) {
            // 接口查询，用户当前模块的按钮权限
            const mjcds = this.getSessionItem("mjCds");
            const path = this.props.history ? this.props.history.location.pathname : '';
            if(!path) return
            const curCd = mjcds.find((item) => item.tzdz === path);
            let id = "";
            if (curCd) {
                id = curCd.id;
            }
            if (id) {
                this.getBtnPermission(id);
            }
        }
    }
}

export default DicComponent;